package leetcode.code0540;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public int singleNonDuplicate(int[] nums) {
		int l = 0, r = nums.length - 1;
		while (l < r) {
			int half = (r - l) >> 1;
			boolean odd = (half & 1) == 1;
			int m = half + l;
			if (nums[m - 1] == nums[m]) {
				if (odd) {
					l = m + 1;
				} else {
					r = m - 2;
				}
			} else if (nums[m + 1] == nums[m]) {
				if (odd) {
					r = m - 1;
				} else {
					l = m + 2;
				}
			} else {
				return nums[m];
			}
		}
		return nums[l];
	}

	@Override
	public void debug1() {
		HelpDebug.compare(2, this.singleNonDuplicate(HelpDebug.str2array3("[1,1,2,3,3,4,4,8,8]")));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(10, this.singleNonDuplicate(HelpDebug.str2array3("[3,3,7,7,10,11,11]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub
		HelpDebug.compare(2, this.singleNonDuplicate(HelpDebug.str2array3("[1,1,2,3,3]")));

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
